package com.example.luoxionghuan.web.controller;

import com.example.luoxionghuan.dao.userMapper;
import com.example.luoxionghuan.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @belongsProject: LuoXiongHuan
 * @belongsPackage: com.example.luoxionghuan.web.controller
 * @author: dongmufengchun
 * @createTime: 2022-06-23  17:52
 * @description: TODO
 * @version: 1.0
 */
@RequestMapping("user")
@Controller
public class UserController {

    @Autowired
    userMapper userMapper;


    @RequestMapping("list")
    @ResponseBody
    public List<User> list(){
        List<User> list = userMapper.list();
        return list;
    }

    @RequestMapping("listhtml")
    public String listhtml(ModelMap map){
        List<User> list = userMapper.list();
        map.put("userList",list);
        return "/user/list";
    }

    @RequestMapping("add")
    public String add(){
        return "/user/add";
    }

    @PostMapping("save")
    public String save(User user){
        userMapper.save(user);
        return "redirect:/user/listhtml";
    }

    @PostMapping("update")
    public String updateSave(User user){
        userMapper.update(user);
        return "redirect:/user/listhtml";
    }

    @GetMapping("update/{id}")
    public String update(@PathVariable("id") String id, ModelMap map){
        User user = userMapper.seleteserById(id);
        map.put("user",user);
        return "/user/update";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable("id") String id){
        userMapper.delete(id);
        return "redirect:/user/listhtml";
    }
}

